#coding:utf-8
'''
@author:        laohu
@date:          2021.07.14
@brief:         资产领用弹出框
@note:          继承自Dialog
'''


from UIComponents.MapLabel import MapLabel
from UserManageDao import UserManageDao
from Infrastructure.AssetStatus import AssetStatus
from ElectronicAssetDAO import ElectronicAssetDAO
from typing import Optional
from PySide6.QtWidgets import QAbstractItemView, QComboBox, QDialog, QDialogButtonBox, QGridLayout, QHBoxLayout, QHeaderView, QLineEdit, QPushButton, QTableWidget, QTableWidgetItem, QWidget
from PySide6.QtCore import Qt
from UIComponents.Dialog.toast import QToast


class RecAssetDialog(QDialog):
    def __init__(self, parent: Optional[QWidget] = None, f: Qt.WindowFlags = Qt.Dialog) -> None:
        super().__init__(parent, f)
        self.resize(800, 500)
        self.setWindowTitle("资产领用")
        self.glayout = QGridLayout(self)
        self.hlayout = QHBoxLayout()
        self.assetNumberLineEdit = QLineEdit()
        self.assetNumberLineEdit.setPlaceholderText("资产编号")
        self.hlayout.addWidget(self.assetNumberLineEdit)
        self.userComboBox = QComboBox()
        self.initUserCBox(self.userComboBox)
        self.hlayout.addWidget(self.userComboBox)
        self.lineEditUsingLocate = QLineEdit()
        self.lineEditUsingLocate.setPlaceholderText("使用位置")
        self.hlayout.addWidget(self.lineEditUsingLocate)
        self.addButton = QPushButton("添加")
        self.addButton.clicked.connect(self.addAsset)
        self.hlayout.addWidget(self.addButton)
        self.glayout.addLayout(self.hlayout, 0, 0)
        self.recAssetsTable = QTableWidget()
        self.recAssetsTable.setSelectionMode(QAbstractItemView.ExtendedSelection)  # 设置只可以单选，可以使用ExtendedSelection进行多选
        self.recAssetsTable.setSelectionBehavior(QAbstractItemView.SelectRows)  # 设置 不可选择单个单元格，只可选择一行。
        self.recAssetsTable.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        self.recAssetsTable.setColumnCount(13)
        self.recAssetsTable.setHorizontalHeaderLabels(
            ['资产编号','设备编号','设备类别','品牌','型号','使用类型','产地','用途','生产日期','使用位置','使用人','备注','操作'])#设置表头文字
        self.glayout.addWidget(self.recAssetsTable, 1, 0)
        self.buttons = QDialogButtonBox()
        self.buttons.addButton("确认", QDialogButtonBox.AcceptRole)
        self.buttons.addButton("取消", QDialogButtonBox.RejectRole)
        self.buttons.accepted.connect(self.accept)
        self.buttons.rejected.connect(self.reject)
        self.glayout.addWidget(self.buttons, 2, 0)
        self.setWindowModality(Qt.ApplicationModal)

    def addAsset(self):
        if self.userComboBox.currentData() is None:
            toast = QToast(self, "请选择使用人", 0, 0, "")
            toast.toast()
            return
        assetNumber = self.assetNumberLineEdit.text()
        for i in range(0, self.recAssetsTable.rowCount()):
            if assetNumber == self.recAssetsTable.cellWidget(i, 0).text():
                toast = QToast(self, "资产编号已存在于待配发列表", 0, 0, "")
                toast.toast()
                return
        assets = ElectronicAssetDAO.search_device(asset_ID = assetNumber, status = AssetStatus.InWarehouse)
        if len(assets) is 0:
            toast = QToast(self, "无对应可配发资产", 0, 0, "")
            toast.toast()
            return
        asset = assets[0]
        row = self.recAssetsTable.rowCount()
        self.recAssetsTable.insertRow(row)
        deviceInfo = MapLabel(asset.asset_ID, asset)
        self.recAssetsTable.setCellWidget(row, 0, deviceInfo)
        self.recAssetsTable.setItem(row, 1, QTableWidgetItem(asset.device_ID))
        self.recAssetsTable.setItem(row, 2, QTableWidgetItem(asset.device_category))
        self.recAssetsTable.setItem(row, 3, QTableWidgetItem(asset.brand))
        self.recAssetsTable.setItem(row, 4, QTableWidgetItem(asset.device_model))
        self.recAssetsTable.setItem(row, 5, QTableWidgetItem(asset.using_type))
        self.recAssetsTable.setItem(row, 6, QTableWidgetItem(asset.origin))
        self.recAssetsTable.setItem(row, 7, QTableWidgetItem(asset.application))
        self.recAssetsTable.setItem(row, 8, QTableWidgetItem(asset.production_date.strftime("%Y\%m\%d")))
        self.recAssetsTable.setItem(row, 9, QTableWidgetItem(self.lineEditUsingLocate.text()))
        userInfo = MapLabel(self.userComboBox.currentText(), self.userComboBox.currentData())
        self.recAssetsTable.setCellWidget(row, 10, userInfo)
        self.recAssetsTable.setItem(row, 11, QTableWidgetItem(asset.remarks))
        buttonDelete = QPushButton("×")
        buttonDelete.clicked.connect(self.buttonDeleteClicked)
        self.recAssetsTable.setCellWidget(row, 12, buttonDelete)
        self.assetNumberLineEdit.setText("")

    def initUserCBox(self, comboBox):
        comboBox.addItem('', None)
        users = UserManageDao.get_all_user()
        for user in users:
            comboBox.addItem(user.name, user)

    def buttonDeleteClicked(self):
        self.recAssetsTable.removeRow(self.recAssetsTable.currentRow())